package com.brainshare.web.controller.an;

import com.brainshare.an.domain.AnWechatUser;
import com.brainshare.common.annotation.Anonymous;
import com.brainshare.common.annotation.RepeatSubmit;
import com.brainshare.common.core.controller.BaseController;
import com.brainshare.common.core.domain.AjaxResult;
import com.brainshare.common.utils.StringUtils;
import com.brainshare.system.domain.AnRedPacketRecord;
import com.brainshare.system.domain.vo.AddRedPacketVO;
import com.brainshare.system.domain.vo.AddWithdrawalCardVO;
import com.brainshare.system.domain.vo.RedPacketInfoVO;
import com.brainshare.system.service.IRedPacketRecordService;
import com.brainshare.system.service.IRedPacketService;
import com.brainshare.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/share/red_packet/v1")
public class RedPacketController extends BaseController {

    @Autowired
    IRedPacketService redPacketService;
    @Autowired
    IRedPacketRecordService redPacketRecordService;

    /**
     * 发红包
     */
    @Anonymous
    @PostMapping("/add_red_packet")
    public AjaxResult addRedPacket(HttpServletRequest request, Integer redPacketType) {
        AddRedPacketVO addRedPacketVO = redPacketService.addRedPacket(request, redPacketType);
        if (null == addRedPacketVO) {
            return error();
        }
        return success(addRedPacketVO);
    }

    /**
     * 领红包
     */
    @Anonymous
    @PostMapping("/receive_red_packet")
    public AjaxResult receiveRedPacket(HttpServletRequest request,Integer redPacketId) {
        Integer result = redPacketService.receiveRedPacket(request, redPacketId);
        if (null==result){
            return error();
        }
        return success(result);
    }

    /**
     * 查询分享次数
     */
    @Anonymous
    @PostMapping("/get_share_time")
    public AjaxResult getShareTime(HttpServletRequest request) {
        Integer shareTime = redPacketService.getShareTime(request);
        return success(shareTime);
    }

    /**
     * 查询红包记录
     */
    @Anonymous
    @PostMapping("/get_red_packet_record")
    public AjaxResult getRedPacketRecord(HttpServletRequest request, Integer redPacketType) {
        AddRedPacketVO addRedPacketVO = redPacketService.getRedPacketRecord(request, redPacketType);
        if (null == addRedPacketVO) {
            return error();
        }
        return success(addRedPacketVO);
    }

    /**
     * 查询红包
     */
    @Anonymous
    @PostMapping("/get_red_packet")
    public AjaxResult getRedPacket(HttpServletRequest request ) {
        RedPacketInfoVO redPacketInfo = redPacketService.getRedPacketInfo(request);
        if (null==redPacketInfo){
            return error();
        }
        return success(redPacketInfo);
    }

    /**
     * 新增提现卡
     */
    @Anonymous
    @PostMapping("/add_withdrawal_card")
    public AjaxResult addWithdrawalCard(HttpServletRequest request,Integer redPacketType) {
        AddWithdrawalCardVO addWithdrawalCardVO = redPacketService.addWithdrawalCard(request, redPacketType);
        if (null==addWithdrawalCardVO){
            return error();
        }
        return success(addWithdrawalCardVO);
    }

    /**
     * 更新提现卡状态
     */
    @Anonymous
    @PostMapping("/update_withdrawal_card")
    public AjaxResult updateWithdrawalCard( HttpServletRequest request,Integer withdrawalCardId,Integer withdrawalCardStatus) {
        return success(redPacketService.updateWithdrawalCard( request,withdrawalCardId,withdrawalCardStatus));
    }

    /**
     * 查询提现卡记录
     */
    @Anonymous
    @PostMapping("/get_withdrawal_card_record")
    public AjaxResult getWithdrawalCardRecord(HttpServletRequest request, Integer redPacketType) {
        AddWithdrawalCardVO addWithdrawalCardVO = redPacketService.getWithdrawalCardRecord(request, redPacketType);
        if (null == addWithdrawalCardVO) {
            return error();
        }
        return success(addWithdrawalCardVO);
    }

    /**
     * 添加筛查领红包步骤
     */
    @Anonymous
    @PostMapping("/add_an_red_packet_step")
    public AjaxResult addAnRedPacketStep(HttpServletRequest request,String router) {
        return success(redPacketService.addAnRedPacketStep(request,router));
    }

    /**
     * 查询筛查领红包当前步骤
     */
    @Anonymous
    @PostMapping("/get_current_an_red_packet_step")
    public AjaxResult getCurrentAnRedPacketStep(HttpServletRequest request) {
        AnRedPacketRecord currentAnRedPacketStep = redPacketService.getCurrentAnRedPacketStep(request);
        if (null == currentAnRedPacketStep) {
            return error();
        }
        return success(currentAnRedPacketStep);
    }

    /**
     * 清理筛查领红包记录
     */
    @Anonymous
    @PostMapping("/clear_an_red_packet_record")
    public AjaxResult clearAnRedPacketRecord() {
        Integer clearResult = redPacketService.clearAnRedPacketRecord();
        if (null == clearResult) {
            return error();
        }
        return success(clearResult);
    }

    /**
     * 发红包接口
     * @return
     */
    @Anonymous
    @RepeatSubmit
    @PostMapping("/sendRedPacket")
    public AjaxResult sendRedPacket(HttpServletRequest request, @RequestBody Map<String, Object> map)
    {
        String orderId = map.get("orderNo").toString();
        AnWechatUser anWechatUser = CommonUtils.getAnWechatUser(request);
        if (StringUtils.isNullOrEmpty(anWechatUser.getOpenId()))
        {
            return AjaxResult.error("用户未授权无法提现");
        }
        boolean result = redPacketRecordService.sendRedPacketToUser(anWechatUser,orderId);
        if (result)
        {
           return AjaxResult.success("红包提现成功！");
        }
        return AjaxResult.error("红包提现失败！");
    }

}
